<?php

namespace XF {

    class CharacterController extends AbstractController {

        public static function Action() {
            URLManager::setPath('character', 'search');
            URLManager::redirect();
        }
        
        public static function searchAction($params) {
            $character = null;
            if (!empty($_POST['charname'])) {
                $character = $_POST['charname'];
            }
            if (!empty($params[0])) {
                $character = $params[0];
            }
            if (!is_null($character)) {
                URLManager::setPath('character', 'view', array($character));
                URLManager::redirect();
            }
            LayoutManager::setView('main', 'character', 'search');
        }

        public static function viewAction($params) {
            Loader::loadModel('Players');

            $Player = new PlayersModel();

            if (!empty($params[0])) {
                $Player->setName($params[0]);
                if (PlayersModel::getByName($Player)) {

                    LayoutManager::setView('main', 'character', 'view');

                    $lastLogin = $Player->getLastlogin();
                    if ($lastLogin == 0) {
                        $lastLogin = 'Never logged in';
                    }
                    else {
                        $lastLogin = strtotime($lastLogin);
                    }
                    
                    
                    LayoutManager::setRepeatID('charinfo');
                    LayoutManager::setRepeatVars(array(
                                array('dataname', 'Name'),
                                array('datavalue', $Player->getName()),
                                array('dataname', 'Sex'),
                                array('datavalue', $Player->getSex() > 0 ? 'Female' : 'Male'),
                                array('dataname', 'Level'),
                                array('datavalue', $Player->getLevel()),
                                array('dataname', 'Last login'),
                                array('datavalue', $lastLogin),
                                array('dataname', 'Status'),
                                array('datavalue', $Player->getOnline() ? 'Online' : 'Offline')
                            ));
                    $vocation = '';
                    $promotion = $Player->getPromotion();
                    switch ($Player->getVocation()) {
                        case 0:
                            $vocation = 'None';
                            break;
                        case 1:
                            if ($promotion)
                                $vocation = 'Elite ';
                            $vocation = 'Knight';
                            break;
                        case 2:
                            if ($promotion)
                                $vocation = '';
                            $vocation = '';
                            break;
                        case 3:
                            if ($promotion)
                                $vocation = '';
                            $vocation = '';
                            break;
                        case 4:
                            if ($promotion)
                                $vocation = '';
                            $vocation = '';
                            break;
                    }
                    LayoutManager::setRepeatVars(array(
                                array('dataname', 'Profession'),
                                array('datavalue', $vocation)
                            ));

                    LayoutManager::setVar('premium', $Player->getPremdays() > 0 ? 'Premium Account' : 'Normal Account');

                    Loader::loadModel('Accounts');

                    $Account = new AccountsModel($Player->getAccountId(), false);

                    $chars = PlayersModel::getByAccount($Account);

                    LayoutManager::setRepeatID('charslist');
                    foreach ($chars as $char) {
                        URLManager::setPath('character', 'view', array($char->getName()));
                        LayoutManager::setRepeatVars(array(
                                    array('link', URLManager::getURI()),
                                    array('level', $char->getLevel()),
                                    array('name', $char->getName()),
                                    array('status', $char->getOnline() ? 'Online' : 'Offline')
                                ));
                    }
                } else {
                    LayoutManager::setView('main', 'character', 'search');
                    LayoutManager::setVar('error-search', 'Character not found.', 'error');
                }
            } else {
                URLManager::setPath('character', 'search');
            }
        }

    }

}
